package com.recallgo.manage.web.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.recallgo.manage.entity.Item;
import com.recallgo.manage.entity.ItemCat;
import com.recallgo.manage.entity.ItemParamItem;
import com.recallgo.manage.entity.vo.PageVO;
import com.recallgo.manage.service.ItemParamItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * <p>
 * 商品规格和商品的关系表 前端控制器
 * </p>
 *
 * @author fendo
 * @since 2018-09-11
 */
@Controller
@RequestMapping("/rest/item/param/item")
public class ItemParamItemController {

    @Autowired
    private ItemParamItemService itemParamItemService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<PageVO> queryItemParamItem(@RequestParam(defaultValue = "1", name = "page") Integer currPage, @RequestParam(defaultValue = "10", name = "rows") Integer size){
        Page<ItemParamItem> page = new Page<>();
        page.setCurrent(currPage);
        page.setSize(size);
        Page<ItemParamItem> itemParamItemPage = itemParamItemService.selectPage(page);
        PageVO<ItemParamItem> pageVO = new PageVO<>();
        pageVO.setTotal((int) itemParamItemPage.getTotal());
        pageVO.setRows(itemParamItemPage.getRecords());
        return ResponseEntity.ok(pageVO);
    }

    /**
     * 根据商品id查询商品规格
     * @param itemId
     * @return
     */
    @RequestMapping(value = "{itemId}", method = RequestMethod.GET)
    public ResponseEntity<ItemParamItem> queryByItemId(@PathVariable("itemId") Long itemId){
        EntityWrapper<ItemParamItem> wrapper = new EntityWrapper<>();
        wrapper.where("item_id = {0}", itemId);
        ItemParamItem itemParamItem = itemParamItemService.selectOne(wrapper);
        if(null == itemParamItem){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(itemParamItem);
    }
}

